package pl.kosmowska.ewelina.client.views.impl;

import java.util.List;

import pl.kosmowska.ewelina.client.ViewMessages;
import pl.kosmowska.ewelina.client.app.requestfactory.dto.SavingsDataProxy;
import pl.kosmowska.ewelina.client.views.SavingsView;
import pl.kosmowska.ewelina.client.views.widgets.MessageDialogBox;
import pl.kosmowska.ewelina.client.views.widgets.dialogs.BooleanDialog;
import pl.kosmowska.ewelina.client.views.widgets.dialogs.BooleanEvent;
import pl.kosmowska.ewelina.client.views.widgets.dialogs.BooleanEvent.Selected;
import pl.kosmowska.ewelina.client.views.widgets.dialogs.BooleanEventHandler;
import pl.kosmowska.ewelina.client.views.widgets.grids.SavingsDataGrid;
import pl.kosmowska.ewelina.client.views.widgets.popup.PopupMessage;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.ResizeLayoutPanel;
import com.google.gwt.user.client.ui.Widget;

public class SavingsViewImpl extends Composite implements SavingsView{

	private static SavingsViewImplUiBinder uiBinder = GWT
			.create(SavingsViewImplUiBinder.class);

	@UiField DockLayoutPanel savingsLayout;
	@UiField ResizeLayoutPanel savingsPanel;
	@UiField Button addSavings;
	@UiField Button editSavings;
	@UiField Button deleteSavings;
	private BooleanDialog wconfirm;
	private SavingsDataProxy selectedItem;

	interface SavingsViewImplUiBinder extends UiBinder<Widget, SavingsViewImpl> {
	}

	private Presenter presenter;
	private boolean alreadyInit;


	private SavingsDataGrid<SavingsDataProxy> savingsGrid;

	public SavingsViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
	}


	public void reInitPagingDataGrid(List<SavingsDataProxy> savingsDataList){
		savingsPanel.clear();
		savingsGrid = new SavingsDataGrid<SavingsDataProxy>();
		savingsGrid.setHeight("600px");
		savingsGrid.setDataList(savingsDataList);			
		savingsPanel.add(savingsGrid);
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}



	@UiHandler("deleteSavings")
	void onDeleteSavingsClick(ClickEvent event) {
		selectedItem= savingsGrid.getSelectedItem();
		if(savingsGrid.getSelectedItem()!=null){
			showDeleteConfirmDialog();
		}else{
			showMessage(ViewMessages.NOTHING_SELECTED_TO_DELETE);
		}

	}
	@UiHandler("editSavings")
	void onEditSavingsClick(ClickEvent event) {
		if(savingsGrid.getSelectedItem()!=null){
			presenter.goToSavingsEditPlace(savingsGrid.getSelectedItem());
		}else{
			showMessage(ViewMessages.NOTHING_SELECTED_TO_EDIT);
		}

	}
	@UiHandler("addSavings")
	void onAddSavingsClick(ClickEvent event) {
		presenter.goToNewSavingsPlace();
	}

	private void showDeleteConfirmDialog() {
		if (wconfirm == null) {
			wconfirm = new BooleanDialog(ViewMessages.CONFIRM_DELETE);
			wconfirm.addSelectionHandler(new BooleanEventHandler() {
				public void onBooleanEvent(BooleanEvent event) {
					if (event.getBooleanEvent() == Selected.TRUE) {
						presenter.deleteSavingsData(selectedItem);
					} else if (event.getBooleanEvent() == Selected.FALSE) {
						// do nothing
					}
				}
			});
		}
		wconfirm.center();
	}


	@Override
	public void reInitView(List<SavingsDataProxy> savingsDataList) {
		System.out.println("SavingsViewImpl.setLoggedIn(): Loading Savings");
		if (savingsDataList == null || savingsDataList.size() == 0) {
			showMessage(ViewMessages.NO_DATA_TO_SHOW);
		}else{
			reInitPagingDataGrid(savingsDataList);
		}

	}


	@Override
	public void updateAfterDelete(SavingsDataProxy savingsDataProxy) {
		savingsGrid.removeFromDataList(selectedItem);
	}


	@Override
	public void showMessage(String errorMessage) {
//		MessageDialogBox messageDialogBox = new MessageDialogBox(errorMessage);
//		messageDialogBox.center();
//		messageDialogBox.show();
        final PopupMessage alertMessage = new PopupMessage(errorMessage);
        alertMessage.startProcessing();
        Timer t = new Timer() {
            public void run() {
                alertMessage.stopProcessing();
            }
        };
        t.schedule(1000);

	}  
	
	@Override
	public boolean isAlreadyInit() {
		return alreadyInit;
	}

	@Override
	public void setAlreadyInit(boolean alreadyInit) {
		this.alreadyInit = alreadyInit;
	}
	
}


